package cn.xchats.rpc.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

/*
 *对象编解码器
 *@author teddy
 *@date 2018/7/4
 */
public class ClientObjectCodecHandler extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        //添加自定义的编码器和解码器
        //添加POJO对象解码器 禁止缓存类加载器
        channel.pipeline().addLast(new ObjectDecoder(1024*1024, ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
        //添加对象编码器 在服务器对外发送消息的时候自动将实现序列化的POJO对象编码
        channel.pipeline().addLast(new ObjectEncoder());
        //处理网络IO  自己配置的处理器
        channel.pipeline().addLast(new TcpClientHander());
    }
}
